package com.baor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baor.dao.AdjustDao;
import com.baor.dao.RoleDao;
import com.baor.dao.ScheduleDao;
import com.baor.dao.UserDao;
import com.baor.domain.AdjustVo;
import com.baor.domain.RoleVo;
import com.baor.domain.ScheduleVo;
import com.baor.domain.UserVo;
import com.baor.service.AdjustService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.baor.enums.Results.*;


@Service
public class AdjustServiceImpl implements AdjustService {

    @Autowired
    private AdjustDao adjustDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private ScheduleDao scheduleDao;

    @Override
    public AjaxResult<?> list(Map<String,Object> map) {
        if ( map.get("pageIndex") == null )
            map.put("pageIndex",1);
        if ( map.get("pageSize") == null)
            map.put("pageSize",10);
        map.put("pageIndex", (int)map.get("pageSize") * ((int)(map.get("pageIndex")) - 1));
        map.put("pageSize", map.get("pageSize"));
        Map<String,Object> res = new HashMap<>();
        res.put("adjustList",adjustDao.list(map));
        res.put("pageCount",adjustDao.page(map));
        return AjaxResult.success(res);
    }

    /**
     * 新添加调课申请
     * 验证：新课程时间无课,存在该课程
     * @param adjustVo 携带 original 与 news
     * @return
     */
    @Override
    public AjaxResult<?> insert(AdjustVo adjustVo) {
        //新时间段是否存在课程
        LambdaQueryWrapper<ScheduleVo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ScheduleVo::getWeekId,adjustVo.getAdjustNewsWeekId())
                .eq(ScheduleVo::getCourseId,adjustVo.getAdjustNewsCourseId());
        List<ScheduleVo> scheduleVos = scheduleDao.selectList(wrapper);
        if ( scheduleVos.size() > 0 ){
            return AjaxResult.error("预调课位置存在课程",scheduleDao.info(Integer.valueOf(scheduleVos.get(0).getScheduleId())));
        }
        LambdaQueryWrapper<ScheduleVo> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ScheduleVo::getWeekId,adjustVo.getAdjustOriginalWeekId())
                .eq(ScheduleVo::getCourseId,adjustVo.getAdjustOriginalCourseId())
                .eq(ScheduleVo::getTeacherId,adjustVo.getTeacherId())
                .eq(ScheduleVo::getClassId,adjustVo.getClassId());
        List<ScheduleVo> scheduleVos2 = scheduleDao.selectList(wrapper2);
        if ( scheduleVos2.size() == 0 ){
            return AjaxResult.error("不存在当前此课程");
        }
        adjustVo.setAdjustStatus("0");
        return AjaxResult.success(adjustDao.insert(adjustVo) > 0 ? INSERT_SUCCESS : INSERT_ERROR);
    }


    /**
     * 审批同意后调用此接口
     * 传入 AdjustVo 实体类，以更新课程表
     * @param adjustVo
     * @return
     */
    @Override
    public AjaxResult<?> updateData(AdjustVo adjustVo){
        LambdaQueryWrapper<ScheduleVo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ScheduleVo::getClassId,adjustVo.getClassId())
                .eq(ScheduleVo::getTeacherId,adjustVo.getTeacherId())
                .eq(ScheduleVo::getWeekId,adjustVo.getAdjustOriginalWeekId())
                .eq(ScheduleVo::getCourseId,adjustVo.getAdjustOriginalWeekId());
        ScheduleVo scheduleVo = scheduleDao.selectList(wrapper).get(0);
        scheduleVo.setWeekId(adjustVo.getAdjustNewsWeekId());
        scheduleVo.setCourseId(adjustVo.getAdjustNewsCourseId());
        return AjaxResult.success(scheduleDao.updateById(scheduleVo) > 0 ? UPDATE_SUCCESS : UPDATE_ERROR);
    }

    /**
     * 更新调课申请内容
     * @param adjustVo
     * @return
     */
    @Override
    public AjaxResult<?> update(AdjustVo adjustVo) {
        LambdaQueryWrapper<ScheduleVo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ScheduleVo::getWeekId,adjustVo.getAdjustNewsWeekId())
                .eq(ScheduleVo::getCourseId,adjustVo.getAdjustNewsCourseId());
        List<ScheduleVo> scheduleVos = scheduleDao.selectList(wrapper);
        if ( scheduleVos.size() > 0 ){
            return AjaxResult.error("预调课位置存在课程",scheduleDao.info(Integer.valueOf(scheduleVos.get(0).getScheduleId())));
        }
        LambdaQueryWrapper<ScheduleVo> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ScheduleVo::getWeekId,adjustVo.getAdjustOriginalWeekId())
                .eq(ScheduleVo::getCourseId,adjustVo.getAdjustOriginalCourseId())
                .eq(ScheduleVo::getTeacherId,adjustVo.getTeacherId())
                .eq(ScheduleVo::getClassId,adjustVo.getClassId());
        List<ScheduleVo> scheduleVos2 = scheduleDao.selectList(wrapper2);
        if ( scheduleVos2.size() == 0 ){
            return AjaxResult.error("不存在当前此课程");
        }
        return AjaxResult.success(adjustDao.updateById(adjustVo) > 0 ? UPDATE_SUCCESS : UPDATE_ERROR);
    }

    @Override
    public AjaxResult<?> superiors(Integer userId) {
        return AjaxResult.success(userDao.superiors(userId));
    }


}
